package com.zq.recruit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.common.exception.ResultException;
import com.zq.recruit.dao.entity.Company;
import com.zq.recruit.dao.entity.JobPosition;
import com.zq.recruit.dao.entity.UserCollection;
import com.zq.recruit.mapper.UserCollectionMapper;
import com.zq.recruit.service.CompanyService;
import com.zq.recruit.service.JobPositionService;
import com.zq.recruit.service.UserCollectionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 张乔
* @description 针对表【user_collection(用户职位收藏表)】的数据库操作Service实现
* @createDate 2025-04-05 19:14:49
*/
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection>
    implements UserCollectionService{

    @Resource
    private UserCollectionMapper userCollectionMapper;


    @Override
    public void getExistById(UserCollection userCollection) {
   Integer count=   userCollectionMapper.getExistById(userCollection);
   if(count>0){
       throw new ResultException(501,"该职位已被收藏，请勿重复操作......");
   }
    }

    @Resource
    private JobPositionService jobPositionService;

    @Resource
    private CompanyService companyService;

    @Override
    public List<JobPosition> getInfoByUserId(Integer id) {
    List<UserCollection> userCollections = userCollectionMapper.getInfoByUserId(id);
if(userCollections==null || userCollections.isEmpty()){
    return null;
}
        List<JobPosition> list = new ArrayList<>();
for(UserCollection userCollection:userCollections){
    JobPosition byId = jobPositionService.getById(userCollection.getJobId());
    Company byId1 = companyService.getById(byId.getCompanyId());
    if(byId1!=null){
        byId.setCompanyName(byId1.getCompanyName());
        byId.setCompanyPath(byId1.getImageUrl());
    }
    byId.setUserCreateTime(userCollection.getCreateTime());
    list.add(byId);
}
        return list;
    }


}




